package com.funtl.framework.web.service.admin.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.funtl.framework.java.common.util.bean.BeanUtil;
import com.funtl.framework.java.exception.DaoException;
import com.funtl.framework.java.exception.ServiceException;
import com.funtl.framework.java.orm.hibernate.service.HibernateParam;
import com.funtl.framework.java.orm.hibernate.service.IHibernateDao;
import com.funtl.framework.web.entity.Admin;
import com.funtl.framework.web.service.admin.IAdminService;

@Component
public class AdminServiceImpl implements IAdminService {
	private IHibernateDao hibernateDao;
	
	@Override
	public Admin login(Admin admin) throws ServiceException {
		try {
			HibernateParam param = new HibernateParam();
			param.setPropertyTypes(new String[] {"loginId", "loginPwd"});
			param.setOperators(new String[] {"=", "="});
			param.setPropertyValues(new String[] {admin.getLoginId(), admin.getLoginPwd()});
			return hibernateDao.get(admin, param);
		} catch (DaoException e) {
			throw new ServiceException("login", e);
		}
	}
	
	//common service

	@Override
	public Admin get(Admin entity) throws ServiceException {
		try {
			HibernateParam param = new HibernateParam();
			param.setPropertyTypes(new String[] {"id"});
			param.setOperators(new String[] {"="});
			param.setPropertyValues(new String[] {entity.getId()});
			return hibernateDao.get(entity, param);
		} catch (DaoException e) {
			throw new ServiceException("get", e);
		}
	}

	@Override
	public Long getCount(Admin entity) throws ServiceException {
		return null;
	}

	@Override
	public List<Admin> getList(Admin entity) throws ServiceException {
		try {
			return hibernateDao.getListByEhcatch(entity, "createDate", "DESC");
		} catch (DaoException e) {
			throw new ServiceException("getList", e);
		}
	}

	@Override
	public String insert(Admin entity) throws ServiceException {
		try {
			hibernateDao.insert(entity);
			return null;
		} catch (DaoException e) {
			throw new ServiceException("新增管理员", e);
		}
	}

	@Override
	public String delete(Admin entity) throws ServiceException {
		try {
			hibernateDao.delete(entity, "id");
			return null;
		} catch (DaoException e) {
			throw new ServiceException("delete", e);
		}
	}

	@Override
	public String update(Admin entity) throws ServiceException {
		try {
			Admin admin = get(entity);
			BeanUtil.beanToBean(entity, admin);
			hibernateDao.update(entity);
			return null;
		} catch (DaoException e) {
			throw new ServiceException("update", e);
		}
	}

	@Override
	public List<Admin> page(Admin entity) throws ServiceException {
		return null;
	}
	
	//getter setter
	
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	@Resource
	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

}
